// pages/repairHome/home.js
const tool = require("../../utils/localStorage.js");
var app = getApp();
Page({
  data: {
    // 轮播
    imgUrls: [],
    autoplay: false,
    interval: 5000,
    duration: 1000,
    // 菜单
    meunList: [
      [{
        'title': '屏幕',
        'imgName': 'screen.png'
      }, {
        'title': '按键',
        'imgName': 'button.png'
      }, {
        'title': '摄像头',
        'imgName': 'camera.png'
      }, {
        'title': '电池',
        'imgName': 'battery.png'
      }],
      [{
        'title': '充电',
        'imgName': 'charging.png'
      }, {
        'title': '进液',
        'imgName': 'leakage.png'
      }, {
        'title': '主板',
        'imgName': 'component.png'
      }, {
        'title': '其他',
        'imgName': 'other.png'
      }]
    ],
    // 当前手机型号
    currentIphoneModel: '',
  },
  /**
   * 轮播图 事件
   */
  changeAutoplay: function(e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function(e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function(e) {
    this.setData({
      duration: e.detail.value
    })
  },
  /**
   * 跳转到 维修界面
   */
  jumpMaintenance(val) {
    wx.navigateTo({
      url: './phoneListSelect/phoneListSelect?maintenance=' + val.currentTarget.dataset.title,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 处理当前 手机型号（针对 苹果手机）
    let currentModel = ''
    if (app.globalData.iphoneInfo.model.indexOf("<") != -1) {
      let arr = app.globalData.iphoneInfo.model.split("<");
      currentModel = arr[0];
      app.globalData.iphoneInfo.model = currentModel;
    }
    this.setData({
      currentIphoneModel: currentModel.length != 0 ? currentModel : app.globalData.iphoneInfo.model
    })

    wx.$api.queryBannerList().then(res => {
      console.log(res)
      if (res.data.code == 10000) {
        this.setData({
          imgUrls: res.data.data.pictures
        })
      }
    })

    // this.getiphoneModel(11)
    // let sanxing = JSON.parse(wx.$localStorage.get('json'))
    // this.defauter(sanxing)
    // console.log(sanxing)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  /**
   * 点击当前手机下单
   */
  currentPhoneOrder() {
    wx.navigateTo({
      url: '/pages/repairHome/orderDetil/orderDetil',
    })

  },
  /**
   * 故障 下单
   */
  malfunctionPhoneOrder(val) {
    if (val.currentTarget.dataset.title) {
      wx.$localStorage.set('malfunctionName', val.currentTarget.dataset.title)
      wx.navigateTo({
        url: '/pages/repairHome/orderDetil/orderDetil?malfunctionName=' + val.currentTarget.dataset.title,
      })
    }

  },
  /**
   * 订单记录
   */
  jumpPage(val) {
    if (val.currentTarget.dataset.type == 'record') {
      wx.navigateTo({
        url: '/pages/repairHome/orderList/orderRecord'
      })
    } else if (val.currentTarget.dataset.type == '内存升级') {
      wx.navigateTo({
        url: '/pages/repairHome/orderDetil/orderDetil?malfunctionName=内存升级',
      })
    } else {
      wx.$util.showModel('手机回收', '请携带手机前往门店，进行回收，谢谢。').then(res => {
        if (res) {
          wx.openLocation({
            latitude: 30.284264,
            longitude: 120.125513,
            name: '古荡湾新村',
            scale: 28
          })
        }
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 默认
  defauter(phones) {
    for (let i = 0; i < phones.length; i++) {
      let item = phones[i];
      var obj = {
        key: 'phone_zhj',
        phoneTypeId: '6',
        typeName: '魅族',

        phoneSeriesId: '0',
        seriesName: '经典系列',

        phoneModelId: String(i),
        modelName: item.model,

        phoneColorId: '1',
        colorName: '黑色'

        // phoneColorId: '0',
        // colorName: '白色'
      }
      console.log(obj)
      // wx.$api.addPhoneInfoData(obj).then(res => {
      //   console.log(res)
      // })
    }
  },
  //系列
  xile() {
    let str = wx.$localStorage.get('phoneJson')
    console.log(str)

    let phones = JSON.parse(str)
    console.log(phones)

    for (let i = 0; i < phones.length; i++) {
      let item = phones[i];
      var obj = {
        key: 'phone_zhj',
        phoneTypeId: '1',
        typeName: '华为',
        phoneSeriesId: String(i),
        seriesName: item.seriesName
      }

      for (let j = 0; j < item.list.length; j++) {
        let el = item.list[j];
        var phone = wx.$tool.mutableCopy(obj);
        phone.phoneModelId = String(j);
        phone.modelName = el.model;
        // phone.phoneColorId = '1';
        // phone.colorName = '黑色';
        phone.phoneColorId = '0';
        phone.colorName = '白色';
        // console.log(phone);
        // console.log(el)
        // wx.$api.addPhoneInfoData(phone).then(res => {
        //   console.log(res)
        // })
      }
    }
  },
  getiphoneModel(id) {
    var self = this;
    wx.request({
      url: 'https://api.jikexiu.com/v1/device/getSeriesAndModelByBrand?brandId=' + id + '&categoryId=12',
      method: 'get',
      success(res) {
        var arr = [];
        // 品类
        for (var j = 0; j < res.data.data.detail.length; j++) {
          let iphoneType = res.data.data.detail[j];
          let obj = {
            seriesName: iphoneType.seriesName,
            list: []
          }
          // 型号
          for (var i = 0; i < iphoneType.devices.length; i++) {
            let item = iphoneType.devices[i];
            let info = {
              model: item.model,
              pic: item.pic,
              id: item.id
            }
            obj.list.push(info)
          }
          arr.push(obj)
        }



        tool.set('sanxing', JSON.stringify(arr))
        // tool.setCurrentUser(arr);
        // for (var j = 0; j < arr.length; j++) {
        //   let el = arr[j];
        //   self.getIphoneColor(el.id)
        // }
      }
    })
  },
  getIphoneColor(id) {
    var self = this;
    wx.request({
      url: 'https://api.jikexiu.com/v1/device/attr/list',
      method: 'post',
      data: {
        deviceId: id
      },
      header: {
        'Origin': 'https://m.jikexiu.com',
        'Access-Control-Request-Headers': 'authorization, x-requested -with',
        'Referer': 'https://m.jikexiu.com/repair/brands?categoryId=12',
        'authorization': 'Basic dGVzdC1qa3g6amt4c2VjcmV0' // 默认值
      },
      success(res) {
        console.log(res)

        // tool.setCurrentUser(arr);

      }
    })
  },
  // 收集 formID
  collectFormId(e) {
    console.log(e.detail.formId)
    wx.$api.collectFormIdData({formId: '234567867645342321234567'}).then(res => {
      console.log(res)
    })
  },
  /**
   *  订单记录
   */
  commitOrder(e) {
    console.log(e.detail.formId)
    wx.navigateTo({
      url: '/pages/repairHome/adminOrderList/adminOrderList'
    })
  },
  /**
   *  订单记录
   */
  bindgetuserinfo(e) {
    
    if (e.detail.userInfo) {
      wx.$localStorage.setCurrentUser(e.detail.userInfo)
      // ivPhoneData: '',
      //   phoneData: '',
      wx.$api.getUserPhone({
        nickName: e.detail.userInfo.nickName,
        headImage: e.detail.userInfo.avatarUrl
      }).then(res => {
        console.log(res)
      })
      wx.navigateTo({
        url: '/pages/repairHome/orderList/orderRecord'
      })
    } else {
      wx.$util.showModel('温馨提示', '请授权微信头像信息,才能查询你的订单记录,谢谢配合');
    }
  },
})